package com.css.devops.monitor;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JenkinsApiMonitor {

	public static void main(String[] args) {
        try {
            String jenkinsUrl = "http://192.168.94.1:18080";
            String pipeline = "fresco-web";
            String branch = "master";
            String buildNumber = "11";
            String nodeId = "40";
            String endpoint = "/blue/rest/organizations/jenkins/pipelines/" + pipeline + "/branches/" + branch + "/runs/" + buildNumber + "/nodes/" + nodeId + "/steps/";

            URL url = new URL(jenkinsUrl + endpoint);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");

            // Set authentication if necessary
            String username = "admin";
            String password = "p@ssw0rd";
            String auth = username + ":" + password;
            String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes());
            connection.setRequestProperty("Authorization", "Basic " + encodedAuth);

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuilder response = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null) {
                    response.append(line);
                }
                in.close();

                // Parse JSON response with Jackson
                ObjectMapper mapper = new ObjectMapper();
                JsonNode steps = mapper.readTree(response.toString());

                for (JsonNode step : steps) {
                    String displayName = step.path("displayName").asText();
                    String result = step.path("result").asText();
                    long durationInMillis = step.path("durationInMillis").asLong();
                    String startTime = step.path("startTime").asText();

                    System.out.println("Step ID: " + step.path("id").asText());
                    System.out.println("Display Name: " + displayName);
                    System.out.println("Result: " + result);
                    System.out.println("Duration: " + durationInMillis + " ms");
                    System.out.println("Start Time: " + startTime);
                    System.out.println();
                }
            } else {
                // Handle HTTP error response
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                StringBuilder errorResponse = new StringBuilder();
                String line;
                while ((line = in.readLine()) != null) {
                    errorResponse.append(line);
                }
                in.close();
                System.out.println("GET request failed. Response Code: " + responseCode);
                System.out.println("Error Response: ");
                System.out.println(errorResponse.toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

